package com.ruoyi.dynamic.controller.app;


import com.ruoyi.common.core.annotation.NoRepeatSubmit;
import com.ruoyi.common.core.annotation.valid.group.ValidGroup;
import com.ruoyi.common.core.constant.constant.PathConstant;
import com.ruoyi.common.core.context.SecurityContextHolder;
import com.ruoyi.common.core.pojo.result.Pages;
import com.ruoyi.common.core.pojo.vo.BaseVO;
import com.ruoyi.common.core.pojo.vo.MultiVO;
import com.ruoyi.common.core.pojo.vo.PageVO;
import com.ruoyi.common.core.pojo.vo.SingleVO;
import com.ruoyi.common.core.result.Result;
import com.ruoyi.dynamic.model.pojo.request.app.*;
import com.ruoyi.dynamic.model.pojo.result.app.*;
import com.ruoyi.dynamic.service.IRoomService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Tag(name = "App-房间相关", description = "App-房间相关")
@RestController
@RequestMapping(PathConstant.APP_PATH + "room")
public class AppRoomController {

    private final IRoomService roomService;

    public AppRoomController(IRoomService roomService) {
        this.roomService = roomService;
    }

    @Operation(summary = "房间列表", description = "房间列表")
    @GetMapping("listPage")
    public PageVO<AppRoomInfoResult> listPage(AppRoomSearchRequest request) {
        Long userId = SecurityContextHolder.getUserId();
        Pages<AppRoomInfoResult> pages = roomService.listPage(request, userId);
        return Result.page(pages);
    }

    @Operation(summary = "更新房间类型", description = "更新房间类型")
    @PostMapping("updateType")
    public BaseVO updateType(@RequestBody @Validated AppRoomUpdateTypeRequest request) {
        Long userId = SecurityContextHolder.getUserId();
        return Result.successOrFail(roomService.updateType(request, userId));
    }

    @NoRepeatSubmit
    @Operation(summary = "创建房间", description = "创建房间")
    @PostMapping("create")
    public SingleVO<AppCreateRoomResult> createRoom(@RequestBody @Validated AppCreateRoomRequest request) {
        Long userId = SecurityContextHolder.getUserId();
        AppCreateRoomResult result = roomService.createRoom(userId, request);
        return Result.single(result);
    }

    @Operation(summary = "房间详情", description = "房间详情")
    @GetMapping("info")
    public SingleVO<AppRoomInfoResult> info(Long id) {
        Long userId = SecurityContextHolder.getUserId();
        AppRoomInfoResult result = roomService.info(userId, id);
        return Result.single(result);
    }

    @Operation(summary = "加入房间", description = "加入房间")
    @GetMapping("enter")
    public SingleVO<AppRoomInfoResult> enter(Long id) {
        AppRoomInfoResult result = roomService.enter(SecurityContextHolder.getUserId(), id);
        return Result.single(result);
    }

    @Operation(summary = "离开房间", description = "离开房间")
    @GetMapping("leave")
    public SingleVO<AppRoomOverResult> leave(@Parameter(description = "房间id", required = true) Long id) {
        AppRoomOverResult result = roomService.leave(id, SecurityContextHolder.getUserId());
        return Result.single(result);
    }

    @Operation(summary = "房间申请上麦/连线", description = "房间申请上麦/连线")
    @PostMapping("apply-mai")
    public BaseVO applyMai(@RequestBody @Validated AppRoomMaiRequest request) {
        Long userId = SecurityContextHolder.getUserId();
        boolean add = roomService.applyMai(userId, request);
        return Result.successOrFail(add);
    }

    @Operation(summary = "房间邀请上麦/连线", description = "房间邀请上麦/连线")
    @PostMapping("invite-mai")
    public BaseVO inviteMai(@RequestBody @Validated AppRoomMaiRequest request) {
        boolean add = roomService.inviteMai(SecurityContextHolder.getUserId(), request);
        return Result.successOrFail(add);
    }

    @Operation(summary = "撤回连线申请", description = "撤回连线申请")
    @PostMapping("recall-mai")
    public BaseVO recallMai(@RequestBody @Validated AppRoomWishCloseRequest request) {
        Long userId = SecurityContextHolder.getUserId();
        boolean add = roomService.recallMai(userId, request);
        return Result.successOrFail(add);
    }

    @Operation(summary = "同意上麦/连线", description = "同意上麦/连线")
    @PostMapping("checkMai")
    public BaseVO checkMai(@RequestBody @Validated AppRoomCheckMaiRequest request) {
        Long userId = SecurityContextHolder.getUserId();
        boolean add = roomService.checkMai(userId, request);
        return Result.successOrFail(add);
    }

    @Operation(summary = "房间上麦/连线", description = "房间上麦/连线")
    @PostMapping("upMai")
    public BaseVO upMai(@RequestBody @Validated AppRoomMaiRequest request) {
        return Result.successOrFail(roomService.upMai(request, SecurityContextHolder.getUserId()));
    }

    @Operation(summary = "房间下麦/连线", description = "房间下麦/连线")
    @PostMapping("downMai")
    public BaseVO downMai(@RequestBody @Validated AppRoomMaiRequest request) {
        return Result.successOrFail(roomService.downMai(request, SecurityContextHolder.getUserId()));
    }

    @Operation(summary = "房间上麦的用户列表", description = "房间上麦的用户列表")
    @GetMapping("maiUsers")
    public MultiVO<AppRoomMaiUserResult> maiUsers(AppRoomProgarmSearchRequest request) {
        Long userId = SecurityContextHolder.getUserId();
        List<AppRoomMaiUserResult> resultList = roomService.maiUsers(request.getRoomId(), request.getType(), request.isRoomUser(), request.isMy(), userId, 0);
        return Result.multi(resultList);
    }

    @Operation(summary = "个人房间封面图", description = "个人房间封面图")
    @GetMapping("cover")
    public MultiVO<String> cover() {
        Long userId = SecurityContextHolder.getUserId();
        return Result.multi(roomService.cover(userId));
    }

    @Operation(summary = "更新房间封面图", description = "更新房间封面图")
    @PostMapping("updateCover")
    public BaseVO updateCover(@RequestBody @Validated({ValidGroup.Insert.class, ValidGroup.Update.class}) AppUpdateRoomCoverRequest request) {
        Long userId = SecurityContextHolder.getUserId();
        return Result.successOrFail(roomService.updateCover(userId, request));
    }



    @Operation(summary = "房间待连线/待上麦列表", description = "房间待连线/待上麦列表")
    @GetMapping("lineListPage")
    public PageVO<AppRoomMaiUserResult> lineListPage(@Validated @ParameterObject AppRoomProgarmSearchRequest pageSearchParam) {
        Long userId = SecurityContextHolder.getUserId();
        Pages<AppRoomMaiUserResult> pages = roomService.lineListPage(pageSearchParam, userId);
        return Result.page(pages);
    }

    @Operation(summary = "取消连线", description = "取消连线")
    @GetMapping("cancelLine")
    public BaseVO cancelLine(@Parameter(description = "房间id", required = true) Long id) {
        Long userId = SecurityContextHolder.getUserId();
        return Result.successOrFail(roomService.cancelLine(id, userId));
    }

    @Operation(summary = "下麦所有玩家", description = "下麦所有玩家")
    @GetMapping("downMaiAll")
    public BaseVO downMaiAll(Long id) {
        Long userId = SecurityContextHolder.getUserId();
        roomService.downMaiAll(id, userId);
        return Result.success();
    }

    @Operation(summary = "下麦单个玩家", description = "下麦单个玩家")
    @PostMapping("downMaiByUser")
    public BaseVO downMaiByUser(@RequestBody @Validated AppRoomDownMaiRequest request) {
        Long userId = SecurityContextHolder.getUserId();
        roomService.downMaiByUser(request, userId);
        return Result.success();
    }

    @Operation(summary = "房间在线用户列表", description = "房间在线用户列表")
    @GetMapping("userListPage")
    public PageVO<AppRoomUserInfoResult> userListPage(@Validated AppRoomUserSearchRequest request) {
        Long userId = SecurityContextHolder.getUserId();
        Pages<AppRoomUserInfoResult> pages = roomService.userListPage(request, userId);
        return Result.page(pages);
    }

    @Operation(summary = "房间内设置静音/解除静音", description = "房间内设置静音/解除静音")
    @PostMapping("isSilence")
    public SingleVO<Integer> isSilence(@RequestBody @Validated AppRoomDownMaiRequest request) {
        Long userId = SecurityContextHolder.getUserId();
        Integer set = roomService.isSilence(request, userId);
        return Result.single(set);
    }

    @Operation(summary = "房间在线人数", description = "房间在线人数")
    @GetMapping("lineNum")
    public SingleVO<Integer> lineNum(Long id) {
        Long userId = SecurityContextHolder.getUserId();
        Integer set = roomService.lineNum(id, userId);
        return Result.single(set);
    }


}

